//
//  AddViewController.m
//  UI11_Contact
//
//  Created by dllo on 15/11/23.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "AddViewController.h"
#import "AddView.h"
#import "Student.h"
#import "DataHandle.h"
#import "ChineseToPinyin.h"

@interface AddViewController ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate>

@property (nonatomic, retain) AddView *addV;

@end

@implementation AddViewController

- (void)dealloc
{
    [_addV release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"添加联系人";
    
    self.addV = [[AddView alloc] initWithFrame:self.view.frame];
    [self.view addSubview:self.addV];
    [_addV release];
    
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(barAction:)] autorelease];
    
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(barAction:)] autorelease];
    
    
}

- (void)barAction:(UIBarButtonItem *)sender
{
    if (sender == self.navigationItem.rightBarButtonItem) {
        Student *stu =  [[Student alloc] init];
        stu.name = self.addV.textF1.text;
        stu.sex = self.addV.textF2.text;
        stu.age = self.addV.textF3.text;
        stu.phone = self.addV.textF4.text;
        stu.headerImage = self.addV.imageV.image;
        
        [self getStuInfo:stu];
        
        
        
        // 将新添加的学生信息传递回去
//        [self.delegate getStuInfo:stu];
        [stu release];
    }
    
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
}

- (void)getStuInfo:(Student *)stu
{
    NSString *key = [[ChineseToPinyin pinyinFromChiniseString:stu.name] substringToIndex:1];
    if (nil != [[DataHandle shareData].stuDic objectForKey:key]) {
        NSMutableArray *arr = [[DataHandle shareData].stuDic objectForKey:key];
        [arr addObject:stu];
    } else {
        NSMutableArray *arr = [NSMutableArray arrayWithObject:stu];
        [[DataHandle shareData].stuDic setObject:arr forKey:key];
        [[DataHandle shareData].stuKeys addObject:key];
        [[DataHandle shareData].stuKeys sortUsingSelector:@selector(compare:)];
    }
//    // 别忘了刷新界面
//    [self.tableV reloadData];
}


// 选好图片触发
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    self.addV.imageV.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
}


- (void)headerImageHandle
{
    // 进入系统相册
    UIImagePickerController *imagePC = [[UIImagePickerController alloc] init];
    imagePC.delegate = self;
    [self presentViewController:imagePC animated:YES completion:^{
        
        
    }];
    [imagePC release];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
